package com.beikbank.modules.baseuser.service.baidu;


import com.beikbank.common.utils.baidu.Base64Util;
import com.beikbank.common.utils.baidu.FileUtil;
import com.beikbank.common.utils.baidu.GsonUtils;
import com.beikbank.common.utils.baidu.HttpUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
* 人脸探测
* @author liyingming
* @data 2017-11-15
*/
//@Service
public class FaceDetectServiceImpl implements FaceDetectService {

    /**
    * 重要提示代码中所需工具类
    * FileUtil,Base64Util,HttpUtil,GsonUtils请从
    * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
    * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
    * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
    * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
    * 下载
    */
    @Override
    public String detect() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v1/detect";
        try {
            // 本地文件路径
            String filePath = "D:/facepic/11.jpg";
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
            String param = "max_face_num=" + 5 + "&face_fields=" + "age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities" + "&image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "请设置你的token";

            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 地址
     * https://aip.baidubce.com/rest/2.0/face/v2/person/verify?access_token=24.f9ba9c5341b67688ab4added8bc91dec.2592000.1485570332.282335-8574074
     * @param arg0
     * @return
     */
    @Override
    public  Map<String, String> detectby(byte[] arg0) {
    	Map<String, String> map = new HashMap<String,String>();
        // 请求url
//        String url = "https://aip.baidubce.com/rest/2.0/face/v1/detect";
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/person/verify";
//        String url = "https://aip.baidubce.com/rest/2.0/face/v3/detect";
//        String url = "https://aip.baidubce.com/rest/2.0/face/v2/person/verify";
        String token = "24.23e9a57d8a98f236cfc77e33e3c4c142.2592000.1532156683.282335-11425704";
        try {
            // 图片数据
            String imgStr = Base64Util.encode(arg0);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = token;

            Map<String, Object> maps = new HashMap<>();
            maps.put("image", imgStr);
            maps.put("name", "王佔晋");
            maps.put("id_card_number", "330881199009224932");
            maps.put("face_field", "faceshape,facetype");
            maps.put("image_type", "BASE64");

            String param = GsonUtils.toJson(maps);

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println(result);

            JSONObject jsonobject = JSONObject.fromObject(result);
            result = jsonobject.getString("result");
            JSONObject jsonobject2 = JSONObject.fromObject(result);
            result = jsonobject2.getString("face_list");

	     	JSONArray json = JSONArray.fromObject(result); // 首先把字符串转成 JSONArray  对象
	     	 for(int i=0;i<json.size();i++){
			        JSONObject job = json.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象

			    	//获取年龄
			        Double ageOne = (Double) job.get("age");
			     	//处理年龄
			        String age =String.valueOf(new BigDecimal(ageOne).setScale(0, BigDecimal.ROUND_HALF_UP));
					map.put("age", age);

					//获取美丑打分
			        Double beautyOne = (Double) job.get("beauty");
					//处理美丑打分
			     	String beauty =String.valueOf(new BigDecimal(beautyOne).setScale(0, BigDecimal.ROUND_HALF_UP));
					map.put("beauty", beauty);

					//获取性别  male(男)、female(女)
					String gender = (String) job.get("gender");
					map.put("gender", gender);

					//获取是否带眼睛 0-无眼镜，1-普通眼镜，2-墨镜
					Integer glasses = (Integer) job.get("glasses");
					map.put("glasses", String.valueOf(glasses));

					//获取是否微笑，0，不笑；1，微笑；2，大笑
					Integer expression = (Integer) job.get("expression");
					map.put("expression", String.valueOf(expression));
			 }

            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        FaceDetectServiceImpl faceDetectService = new FaceDetectServiceImpl();

        faceDetectService.detect();
    }
}